package com.leetcode.partition4;

import java.util.Arrays;
import java.util.Random;

/**
 * @author `RKC`
 * @date 2021/11/7 9:14
 */
public class LC384打乱数组 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        Solution solution = new Solution(nums);
        System.out.println(Arrays.toString(solution.shuffle()));
        System.out.println(Arrays.toString(solution.shuffle()));
        System.out.println(Arrays.toString(solution.shuffle()));
    }

    private static class Solution {
        private int[] nums;

        public Solution(int[] nums) {
            this.nums = nums;
        }

        public int[] reset() {
            return nums;
        }

        public int[] shuffle() {
            Random random = new Random();
            int[] res = nums.clone();
            //n个元素打乱只需要随机交换n-1次
            for (int i = 0; i < res.length; i++) {
                //随机生成[i, res.length)范围的索引
                int r = random.nextInt(res.length - i) + i;
                int temp = res[r];
                res[r] = res[i];
                res[i] = temp;
            }
            return res;
        }
    }
}
